package com.kakaxi.mybilibili.adapter;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.daimajia.slider.library.SliderLayout;
import com.kakaxi.mybilibili.R;
import com.kakaxi.mybilibili.adapter.entity.AdviceHomeMultiItem;
import com.kakaxi.mybilibili.bean.AdviceHomeItemBean;
import com.kakaxi.mybilibili.bean.BannerItemBean;
import com.kakaxi.mybilibili.utils.CastUncheckedUtil;
import com.kakaxi.mybilibili.utils.SliderLayoutHelper;
import com.kakaxi.mybilibili.view.GridMarginDecoration;

import java.util.ArrayList;
import java.util.List;

/**
 * 推荐主页面复杂类型列表适配器
 * Created by kakaxi on 16/11/19.
 */

public class AdviceHomeRecyAdapter extends BaseMultiItemQuickAdapter<AdviceHomeMultiItem,
        BaseViewHolder> {

    /** 轮播是否初始化 **/
    private boolean bannerIsNotInit = true;

    public AdviceHomeRecyAdapter(List<AdviceHomeMultiItem> data) {
        super(data);
        addItemType(AdviceHomeMultiItem.TYPE_HEADER_NORMAL, R.layout.item_recy_advice_home_header);
        addItemType(AdviceHomeMultiItem.TYPE_HEADER_BANNER, R.layout.item_recy_common_banner);
        addItemType(AdviceHomeMultiItem.TYPE_HEADER_TOP, R.layout.item_recy_advice_home_header);
        addItemType(AdviceHomeMultiItem.TYPE_HEADER_LIVE, R.layout.item_recy_live_partition);
        addItemType(AdviceHomeMultiItem.TYPE_HEADER_DRAMA, R.layout.item_recy_advice_home_header);
        addItemType(AdviceHomeMultiItem.TYPE_HEADER_WEBLINK, R.layout.item_recy_advice_home_header);
        addItemType(AdviceHomeMultiItem.TYPE_HEADER_CENTER, R.layout.item_recy_advice_home_header);
        addItemType(AdviceHomeMultiItem.TYPE_CONTENT_NORMAL, R.layout
                .item_recy_advice_home_content);
        addItemType(AdviceHomeMultiItem.TYPE_CONTENT_LIVE, R.layout.item_recy_live_content);
        addItemType(AdviceHomeMultiItem.TYPE_CONTENT_DRAMA, R.layout.item_recy_advice_home_drama);
        addItemType(AdviceHomeMultiItem.TYPE_CONTENT_CENTER, R.layout.item_recy_live_content);
        addItemType(AdviceHomeMultiItem.TYPE_CONTENT_WEBLINK, R.layout
                .item_recy_advice_home_weblink);
    }

    @Override
    protected void convert(BaseViewHolder baseViewHolder, AdviceHomeMultiItem item) {
        switch (item.getItemType()) {
            case AdviceHomeMultiItem.TYPE_HEADER_BANNER: // 轮播标题
                onBindHeaderBannerViewHolder(baseViewHolder, item.getData());
                break;
            case AdviceHomeMultiItem.TYPE_HEADER_TOP: // 推荐标题
                onBindHeaderTopViewHolder(baseViewHolder, item.getData());
                break;
            case AdviceHomeMultiItem.TYPE_HEADER_LIVE: // 直播标题
                onBindHeaderLiveViewHolder(baseViewHolder, item.getData());
                break;
            case AdviceHomeMultiItem.TYPE_HEADER_DRAMA: // 番剧标题
                onBindHeaderDramaViewHolder(baseViewHolder, item.getData());
                break;
            case AdviceHomeMultiItem.TYPE_HEADER_WEBLINK: // 话题标题
                onBindHeaderWeblinkViewHolder(baseViewHolder, item.getData());
                break;
            case AdviceHomeMultiItem.TYPE_HEADER_CENTER: // 活动中心标题
                onBindHeaderCenterViewHolder(baseViewHolder, item.getData());
                break;
            case AdviceHomeMultiItem.TYPE_HEADER_NORMAL: // 通用标题
                onBindHeaderNormalViewHolder(baseViewHolder, item.getData());
                break;
            case AdviceHomeMultiItem.TYPE_CONTENT_LIVE: // 直播正文
                onBindContentLiveViewHolder(baseViewHolder, item.getData());
                break;
            case AdviceHomeMultiItem.TYPE_CONTENT_DRAMA: // 番剧正文
                onBindContentDramaViewHolder(baseViewHolder, item.getData());
                break;
            case AdviceHomeMultiItem.TYPE_CONTENT_CENTER: // 活动中心正文
                onBindContentCenterViewHolder(baseViewHolder, item.getData());
                break;
            case AdviceHomeMultiItem.TYPE_CONTENT_WEBLINK: // 话题正文
                onBindContentWeblinkViewHolder(baseViewHolder, item.getData());
                break;
            case AdviceHomeMultiItem.TYPE_CONTENT_NORMAL: // 通用正文
                onBindContentNormalViewHolder(baseViewHolder, item.getData());
                break;
        }
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        if (recyclerView.getLayoutManager() == null) {
            // 设置格子布局管理器
            GridLayoutManager layoutManager = new GridLayoutManager(recyclerView.getContext(), 2);
            // 设置单列和满列
            layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    int spanSize = 1;
                    switch (getItemViewType(position)) {
                        case AdviceHomeMultiItem.TYPE_HEADER_BANNER:
                        case AdviceHomeMultiItem.TYPE_HEADER_TOP:
                        case AdviceHomeMultiItem.TYPE_HEADER_LIVE:
                        case AdviceHomeMultiItem.TYPE_HEADER_DRAMA:
                        case AdviceHomeMultiItem.TYPE_HEADER_WEBLINK:
                        case AdviceHomeMultiItem.TYPE_HEADER_CENTER:
                        case AdviceHomeMultiItem.TYPE_HEADER_NORMAL:
                        case AdviceHomeMultiItem.TYPE_CONTENT_WEBLINK:
                        case AdviceHomeMultiItem.TYPE_CONTENT_CENTER:
                            spanSize = 2;
                            break;
                    }
                    return spanSize;
                }
            });
            recyclerView.setLayoutManager(layoutManager);
        }

        // 设置正文的边距
        List<Integer> typeList = new ArrayList<>();
        typeList.add(AdviceHomeMultiItem.TYPE_CONTENT_NORMAL);
        typeList.add(AdviceHomeMultiItem.TYPE_CONTENT_DRAMA);
        typeList.add(AdviceHomeMultiItem.TYPE_CONTENT_LIVE);
        typeList.add(AdviceHomeMultiItem.TYPE_CONTENT_WEBLINK);
        recyclerView.addItemDecoration(new GridMarginDecoration(recyclerView.getContext(),
                typeList, GridMarginDecoration.DEFAULT_MARGIN_DP));
    }

    /**
     * 绑定轮播数据
     *
     * @param baseViewHolder 持久化视图
     * @param data           数据
     */
    private void onBindHeaderBannerViewHolder(BaseViewHolder baseViewHolder, Object data) {
        // 初始化轮播
        SliderLayout sliderLayout = baseViewHolder.getView(R.id.sl_common_banner);
        if (bannerIsNotInit) {
            SliderLayoutHelper.initSliderLayout(sliderLayout);
            bannerIsNotInit = false;
        }
        // 设置轮播数据
        List<BannerItemBean> bannerItemBeanList = CastUncheckedUtil.cast(data);
        SliderLayoutHelper.setUrlSlider(sliderLayout, bannerItemBeanList, null);
    }

    /**
     * 绑定推荐标题数据
     *
     * @param baseViewHolder 持久化视图
     * @param data           数据
     */
    private void onBindHeaderTopViewHolder(BaseViewHolder baseViewHolder, Object data) {
        AdviceHomeItemBean.AdviceHomeHeadItemBean headItemBean = (AdviceHomeItemBean
                .AdviceHomeHeadItemBean) data;
        GridLayoutManager.LayoutParams layoutParams = (GridLayoutManager.LayoutParams)
                baseViewHolder.itemView.getLayoutParams();
        layoutParams.topMargin = (int) mContext.getResources().getDimension(R.dimen
                .default_distance);
        baseViewHolder
                .setText(R.id.txt_advice_home_header_name, headItemBean.getTitle())
                .setText(R.id.roubtn_advice_home_header_go, "排行榜");
    }

    /**
     * 绑定番剧标题数据
     *
     * @param baseViewHolder 持久化视图
     * @param data           数据
     */
    private void onBindHeaderDramaViewHolder(BaseViewHolder baseViewHolder, Object data) {
        AdviceHomeItemBean.AdviceHomeHeadItemBean headItemBean = (AdviceHomeItemBean
                .AdviceHomeHeadItemBean) data;
        baseViewHolder
                .setText(R.id.txt_advice_home_header_name, headItemBean.getTitle())
                .setText(R.id.roubtn_advice_home_header_go, "更多");
    }

    /**
     * 绑定直播标题数据
     *
     * @param baseViewHolder 持久化视图
     * @param data           数据
     */
    private void onBindHeaderLiveViewHolder(BaseViewHolder baseViewHolder, Object data) {
        AdviceHomeItemBean.AdviceHomeHeadItemBean headItemBean = (AdviceHomeItemBean
                .AdviceHomeHeadItemBean) data;
        String countStr = "当前<font color='#FF0000'>" + headItemBean.getCount() + "</font>个直播>";
        baseViewHolder
                .setText(R.id.txt_live_partition_name, headItemBean.getTitle())
                .setText(R.id.txt_live_partition_num, Html.fromHtml(countStr));
    }

    /**
     * 绑定话题标题数据
     *
     * @param baseViewHolder 持久化视图
     * @param data           数据
     */
    private void onBindHeaderWeblinkViewHolder(BaseViewHolder baseViewHolder, Object data) {
        AdviceHomeItemBean.AdviceHomeHeadItemBean headItemBean = (AdviceHomeItemBean
                .AdviceHomeHeadItemBean) data;
        baseViewHolder.setText(R.id.txt_advice_home_header_name, "话题");
    }

    /**
     * 绑定通用标题数据
     *
     * @param baseViewHolder 持久化视图
     * @param data           数据
     */
    private void onBindHeaderCenterViewHolder(BaseViewHolder baseViewHolder, Object data) {
        AdviceHomeItemBean.AdviceHomeHeadItemBean headItemBean = (AdviceHomeItemBean
                .AdviceHomeHeadItemBean) data;
    }

    /**
     * 绑定通用标题数据
     *
     * @param baseViewHolder 持久化视图
     * @param data           数据
     */
    private void onBindHeaderNormalViewHolder(BaseViewHolder baseViewHolder, Object data) {
        AdviceHomeItemBean.AdviceHomeHeadItemBean headItemBean = (AdviceHomeItemBean
                .AdviceHomeHeadItemBean) data;
        baseViewHolder
                .setText(R.id.txt_advice_home_header_name, headItemBean.getTitle())
                .setText(R.id.roubtn_advice_home_header_go, "进去看看");
    }

    /**
     * 绑定直播正文数据
     *
     * @param baseViewHolder 持久化视图
     * @param data           数据
     */
    private void onBindContentLiveViewHolder(BaseViewHolder baseViewHolder, Object data) {
        AdviceHomeItemBean.AdviceHomeBodyItemBean bodyItemBean = (AdviceHomeItemBean
                .AdviceHomeBodyItemBean) data;
        baseViewHolder
                .setText(R.id.txt_live_content_title, bodyItemBean.getTitle())
                .setText(R.id.txt_live_content_own_name, bodyItemBean.getUp())
                .setText(R.id.txt_live_content_online, bodyItemBean.getOnline() + "");
        Glide.with(mContext).load(bodyItemBean.getCover()).crossFade().into((ImageView)
                baseViewHolder.getView(R.id.srimg_live_content_cover));
    }

    /**
     * 绑定番剧正文数据
     *
     * @param baseViewHolder 持久化视图
     * @param data           数据
     */
    private void onBindContentDramaViewHolder(BaseViewHolder baseViewHolder, Object data) {
        AdviceHomeItemBean.AdviceHomeBodyItemBean bodyItemBean = (AdviceHomeItemBean
                .AdviceHomeBodyItemBean) data;
        baseViewHolder
                .setText(R.id.txt_advice_home_drama_title, bodyItemBean.getTitle())
                .setText(R.id.txt_advice_home_drama_desc, bodyItemBean.getDesc1());
        Glide.with(mContext).load(bodyItemBean.getCover()).crossFade().into((ImageView)
                baseViewHolder.getView(R.id.srimg_advice_home_drama_cover));
    }

    /**
     * 绑定活动中心正文数据
     *
     * @param baseViewHolder 持久化视图
     * @param data           数据
     */
    private void onBindContentCenterViewHolder(BaseViewHolder baseViewHolder, Object data) {

    }

    /**
     * 绑定轮播数据
     *
     * @param baseViewHolder 持久化视图
     * @param data           数据
     */
    private void onBindContentWeblinkViewHolder(BaseViewHolder baseViewHolder, Object data) {
        AdviceHomeItemBean.AdviceHomeBodyItemBean bodyItemBean = (AdviceHomeItemBean
                .AdviceHomeBodyItemBean) data;
        Glide.with(mContext).load(bodyItemBean.getCover()).crossFade().into((ImageView)
                baseViewHolder.getView(R.id.srimg_advice_home_weblink_cover));
    }

    /**
     * 绑定轮播数据
     *
     * @param baseViewHolder 持久化视图
     * @param data           数据
     */
    private void onBindContentNormalViewHolder(BaseViewHolder baseViewHolder, Object data) {
        AdviceHomeItemBean.AdviceHomeBodyItemBean bodyItemBean = (AdviceHomeItemBean
                .AdviceHomeBodyItemBean) data;
        baseViewHolder
                .setText(R.id.txt_advice_home_content_title, bodyItemBean.getTitle())
                .setText(R.id.txt_advice_home_content_play, bodyItemBean.getPlay())
                .setText(R.id.txt_advice_home_content_danmaku, bodyItemBean.getDanmaku());
        Glide.with(mContext).load(bodyItemBean.getCover()).crossFade().into((ImageView)
                baseViewHolder.getView(R.id.srimg_advice_home_content_cover));
    }

}
